package cn.stronglink.asset.module.basicdata.assetBrand.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.plugins.Page;

import cn.stronglink.asset.model.AssetBrand;
import cn.stronglink.asset.module.basicdata.assetBrand.service.AssetBrandService;
import cn.stronglink.suite.core.base.AbstractController;
import cn.stronglink.suite.core.config.Resources;
import cn.stronglink.suite.core.log.annotation.OperateLog;
import cn.stronglink.suite.core.log.annotation.OperateLog.OpType;
import cn.stronglink.suite.core.support.HttpCode;

@Controller
@RequestMapping(value = "basic/assetBrand")
public class AssetBrandController extends AbstractController {

	@Autowired
	private AssetBrandService assetBrandService;


	@RequestMapping(value = "queryList")
	public ResponseEntity<ModelMap> queryList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Map<String, Object> params) {		
		String name = params.get("name").toString();
		@SuppressWarnings("unchecked")
		Page<AssetBrand> page = (Page<AssetBrand>) super.getPage(params);
		Page<AssetBrand> data = assetBrandService.queryList(page, name);
		return setSuccessModelMap(modelMap, data);
	}
	
	@RequestMapping(value = "queryAssetBrand")
	public ResponseEntity<ModelMap> queryAssetBrand(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response) {		
		List<AssetBrand> data = assetBrandService.queryAssetBrandList();
		return setSuccessModelMap(modelMap, data);
	}
	
	@OperateLog(module = "资产品牌",desc="增加品牌", type = OpType.ADD)
	@RequestMapping(value = "create")
	public ResponseEntity<ModelMap> create(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody AssetBrand assetBrand) {
		assetBrandService.insertAssetBrand(assetBrand);
		return setSuccessModelMap(modelMap, null);
	}
	
	@OperateLog(module = "资产品牌",desc="修改品牌信息", type = OpType.UPDATE)
	@RequestMapping(value = "edit")
	public ResponseEntity<ModelMap> edit(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody AssetBrand assetBrand) {
		assetBrandService.insertAssetBrand(assetBrand);
		return setSuccessModelMap(modelMap, null);
	}
	
	@OperateLog(module = "资产品牌",desc="删除品牌信息", type = OpType.DEL)
	@RequestMapping(value = "remove")
	public ResponseEntity<ModelMap> remove(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Long id) {
		int isUsed =assetBrandService.getIsUsedBrand(id);
		if(isUsed > 0){		
			return setModelMap(modelMap, HttpCode.CONFLICT, Resources.getMessage("IsUsedBrand"));
		}
		assetBrandService.removeAssetBrand(id);
		return setSuccessModelMap(modelMap, null);
	}

}
